package com.example.shardingjdbc.hint;

import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author: gongjunjie
 * @date: 2022/3/31
 */
public class MyHintShardingAlgorithm implements HintShardingAlgorithm<DataBaseEnum> {

    @Override
    public Collection<String> doSharding(Collection<String> dataBaseNames, HintShardingValue<DataBaseEnum> hintShardingValue) {
        Collection<String> resultSet = new ArrayList<>();

        for (String dataBaseName : dataBaseNames) {
            Collection<DataBaseEnum> values = hintShardingValue.getValues();
            for (DataBaseEnum value : values) {
                if(dataBaseName.equals(value.getDataBaseName())){
                    resultSet.add(dataBaseName);
                }
            }
        }
        return resultSet;
    }
}
